/**
 * @param {number[]} nums
 * @return {number[]}
 */
var nextGreaterElements = function(nums) {
    result=new Array(nums.length);
    stack=new Array();
    var n=nums.length;
    for(let i=2*n-1;i>=0;i--){
        if(stack.length==0){
            stack.push(nums[i%n]);
        }
        else if(nums[i%n]>=stack[stack.length-1]){
            while(stack.length>0&&nums[i%n]>=stack[stack.length-1]){
                stack.pop()
            }
            if(stack.length=0){
                result[i%n]=-1
            }
            else{
                result[i%n]=stack[stack.length-1]
            }
            stack.push(nums[i%n]);
        }
        else{
            result[i%n]=stack[stack.length-1];
            stack.push(nums[i%n]);
        }
    }
    return result;
};

